package server.business.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.catalina.Server;
import org.springframework.web.bind.annotation.*;
import server.business.pojo.BloodInbound;
import server.business.pojo.BloodStock;
import server.business.service.BloodInBoundService;
import server.business.service.BloodStockService;
import server.business.utils.results.fzx.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author FZX
 * @since 2022-08-01
 */
@Controller
@RequestMapping("/bloodStocks")
public class BloodStocksController {
    @Autowired
    private BloodStockService bloodStockService;
    @Autowired
    private BloodInBoundService BloodInboundService;
    /**
     * 用于获取所有的库存信息
     * @return 更新后的所有的血液库存
     * @dictionary RhType/AboType/States
     */
    @ResponseBody

    @GetMapping("/normal/getBloodInfoAll/{currentPage}/{pageSize}")
    public Result getPage(@PathVariable int currentPage, @PathVariable int pageSize, BloodStock user){

        IPage<BloodStock> page = bloodStockService.listBlood(currentPage, pageSize,user);
        System.out.println(page);
        //如果当前页码值大于了总页码值，就重新执行查询操作，使用最大页码值作为当前页码值
        if( currentPage > page.getPages()){
            page = bloodStockService.listBlood((int)page.getPages(), pageSize,user);

        }
        return Result.success(page);}
    /**
     * 血型筛选库存
     * @return 符合要求的血液库存
     * @dictionary RhType/AboType/State
     */
    @ResponseBody
    @GetMapping("/normal/getBloodAboRh")
    public Result getBloodAboRh(@RequestParam(value = "abo") String abo,
                                @RequestParam(value = "rh") String rh,
                                @RequestParam(value = "except") String except) {

        List<BloodStock> bloodList= bloodStockService.listBloodAboRh(abo,rh,except);
        if (bloodList != null) {
            System.out.println(bloodList);
            return Result.success(bloodList);
        } else {
            return Result.fail();
        }
    }
    /**
     * 状态筛选库存
     * @return 符合要求的血液库存
     * @dictionary RhType/AboType/State
     */
    @ResponseBody
    @GetMapping("/normal/getBloodUseful/{currentPage}/{pageSize}")
    public Result getBloodUseful(@PathVariable int currentPage, @PathVariable int pageSize,@RequestParam(value = "bloodCon") String bloodCon) {
        IPage<BloodStock> page = bloodStockService.listBloodUseful(currentPage, pageSize,bloodCon);

        System.out.println(page);
        //如果当前页码值大于了总页码值，就重新执行查询操作，使用最大页码值作为当前页码值
        if( currentPage > page.getPages()){
            page = bloodStockService.listBloodUseful((int)page.getPages(), pageSize,bloodCon);

        }
        return Result.success(page);
    }
    /**
     * 库存分析
     * @return 各种类型的血液库存结果
     * @dictionary RhType/AboType/State
     */
    @ResponseBody
    @GetMapping("/normal/getVarityNumber")
    public Result getVarityNumber() {
        List<Map<String, Object>> bloodleft= bloodStockService.getVarityNumber();
        if (bloodleft != null) {
            System.out.println(bloodleft);
            return Result.success(bloodleft);
        } else {
            return Result.fail();
        }
    }
    /**
     * 库存查找
     * @return 指定血液情况
     * @dictionary RhType/AboType/State
     */
    @ResponseBody
    @GetMapping("/normal/getBloodInfoAlone")
    public Result getBloodInfoAlone(@RequestParam(value = "bloodId") String bloodId) {
        List<BloodStock> bloodListAlone= bloodStockService.listBloodAlone(bloodId);
        if (bloodListAlone != null) {
            System.out.println(bloodListAlone);
            return Result.success(bloodListAlone);
        } else {
            return Result.fail();
        }
    }

    @ResponseBody
    @GetMapping("/normal/getBloodInBound")
    public Result getBloodInbound(@RequestParam(value = "bloodId") String bloodId) {
        List<Map<String, Object>> BloodInList= BloodInboundService.getBloodInList(bloodId);
        if (BloodInList != null) {
            System.out.println(BloodInList);
            return Result.success(BloodInList);
        } else {
            return Result.fail();
        }
    }
}
